package com.fantastic.service.zjs.impl;

import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.exception.sql.DeleteException;
import com.fantastic.common.exception.sql.InsertException;
import com.fantastic.common.exception.sql.SelectException;
import com.fantastic.common.exception.sql.UpdateException;
import com.fantastic.common.utils.FilesUtils;
import com.fantastic.common.utils.IsEmptyUtils;
import com.fantastic.common.utils.MultipartFileUtils;
import com.fantastic.mapper.zjs.CouponsMapper;
import com.fantastic.model.pojo.zjs.Advertise;
import com.fantastic.model.pojo.zjs.Coupons;
import com.fantastic.service.zjs.CouponsService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: rxx
 * @since: 2024/6/3 09:07
 * @description: 优惠券信息表service接口实现类
 */
@Service
public class CouponsServiceImpl implements CouponsService {

    @Resource
    private CouponsMapper couponsMapper;

    /**
     * 查询优惠券信息
     *
     * @param coupons
     * @return
     */
    @Override
    public List<Coupons> listCoupons(Coupons coupons) {
        List<Coupons> couponsList = couponsMapper.listCoupons(coupons);
        IsEmptyUtils.listIsEmpty(new SelectException(RespStatusInfo.SELECT_NULL), couponsList);
        for (Coupons list : couponsList) {
            list.setCouponPicture(FilesUtils.getSlashParse(list.getCouponPicture()));
        }
        return couponsList;
    }

    /**
     * 查询单个优惠券信息
     *
     * @param couponId
     * @return
     */
    @Override
    public Coupons getCoupons(Integer couponId) {
        IsEmptyUtils.integerIsEmpty(new SelectException(RespStatusInfo.DATA_ERROR), couponId);
        Coupons coupons = couponsMapper.getCoupons(couponId);
        coupons.setCouponPicture(FilesUtils.getSlashParse(coupons.getCouponPicture()));
        return coupons;
    }

    /**
     * 添加优惠券信息
     *
     * @param filePicture
     * @param coupons
     * @return
     */
    @Override
    public Integer insertCoupons(MultipartFile filePicture, Coupons coupons) {
        //判断优惠券类型id、优惠卷状态、是否支持外送状态
        IsEmptyUtils.integerIsEmpty(new InsertException(RespStatusInfo.DATA_ERROR), coupons.getCouponTypeId(), coupons.getCouponStatus(), coupons.getCouponGoStatus());
        //判断优惠券名称、优惠券说明
        IsEmptyUtils.stringIsEmpty(new InsertException(RespStatusInfo.DATA_ERROR), coupons.getCouponName(), coupons.getCouponRemark());
        //是否有开始时间和结束时间
        IsEmptyUtils.objectIsNull(new InsertException(RespStatusInfo.DATA_ERROR), coupons.getCouponStartTime(), coupons.getCouponEndTime());
        //判断文件处理
        if (filePicture != null && filePicture.getSize() != 0) {
            MultipartFileUtils.supportType(filePicture.getOriginalFilename());
            String relativePath = MultipartFileUtils.writeFile(filePicture, "优惠券");
            coupons.setCouponPicture(relativePath);
        } else {
            throw new InsertException(RespStatusInfo.FILETYPE_NULL);
        }
        return couponsMapper.insertCoupons(coupons);
    }

    /**
     * 修改优惠券信息
     *
     * @param filePicture
     * @param coupons
     * @return
     */
    @Override
    public Integer updateCoupons(MultipartFile filePicture, Coupons coupons) {
        IsEmptyUtils.integerIsEmpty(new UpdateException(RespStatusInfo.DATA_ERROR), coupons.getCouponId());
        //文件不为空
        if (filePicture != null && filePicture.getSize() != 0) {
            MultipartFileUtils.supportType(filePicture.getOriginalFilename());
            String relativePath = MultipartFileUtils.writeFile(filePicture, "优惠券");
            coupons.setCouponPicture(relativePath);
            //进行原有图片删除
            Coupons getCoupons = getCoupons(coupons.getCouponId());
            MultipartFileUtils.deleteFile(getCoupons.getCouponPicture());
        }
        return couponsMapper.updateCoupons(coupons);
    }

    /**
     * 删除优惠券信息
     *
     * @param couponId
     * @return
     */
    @Override
    public Integer deleteCoupons(Integer couponId) {
        IsEmptyUtils.integerIsEmpty(new DeleteException(RespStatusInfo.DATA_ERROR), couponId);
        return couponsMapper.deleteCoupons(couponId);
    }

}
